<div ng-controller="Purchasing_CommodityInfoEditC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">修改采购单商品实重</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="container-fluid text-center bg-light lter" >
                            <div class="row">
                                <h3 style="display: inline-block;margin: 0 auto">采购单商品列表</h3>
                            </div>
                        </div>
                        <div class="container-fluid">
                            <!--<div class="row" style="border-bottom: red solid 1px" ng-repeat="item in orderdetaildata.Goodses">-->
                                <!--<div class="col-xs-9 col-md-9">-->
                                    <!--<div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>-->
                                    <!--<div>单价：-->
                                        <!--<span ng-show="isedit">{{item.Price}}</span>-->
                                        <!--<input ng-show="!isedit" type="number" min="0" ng-model="item.Price" required>-->
                                    <!--</div>-->
                                    <!--<div>数量：-->
                                        <!--{{item.Count}}-->
                                    <!--</div>-->
                                    <!--<div>总质量：-->
                                        <!--{{item.Weight}}t-->
                                    <!--</div>-->
                                    <!--<div>总价：{{item.Weight}}*{{item.Price}}={{item.Weight*item.Price}}</div>-->
                                <!--</div>-->
                                <!--<div class="col-xs-3 col-md-3" style="text-align: center;">-->
                                    <!--<button type="button" ng-show="isedit" class="btn btn-sm btn-success"  ng-click="edit(item)" style="margin: 5px">-->
                                        <!--编辑-->
                                    <!--</button>-->
                                    <!--<button type="button" ng-show="!isedit" class="btn btn-sm btn-success"  ng-click="edit(item)" style="margin: 5px">-->
                                        <!--完成-->
                                    <!--</button>-->
                                <!--</div>-->
                            <!--</div>-->


                            <div class="panel panel-default" ng-repeat="item in orderdetaildata.Goodses" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>品牌：{{item.Commodity.Brand}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-9 col-md-9">
                                            <div>名称/材质/规格：{{item.Commodity.Name}}/{{item.Commodity.Standard}}/{{item.Commodity.Format}}</div>
                                            <div>单价：
                                                <span ng-show="isedit">{{item.Price | number:2}}</span>
                                                <input ng-show="!isedit" type="number" min="0" ng-model="item.Price" required>
                                            </div>
                                            <div>数量：
                                                {{item.Count}}
                                            </div>
                                            <div>总质量：
                                                {{item.Weight | number:3}}t
                                            </div>
                                        </div>
                                        <div class="col-xs-3 col-md-3" style="text-align: center;">
                                            <button type="button" ng-show="isedit" class="btn btn-sm btn-success"  ng-click="edit(item)" style="margin: 5px">
                                                编辑
                                            </button>
                                            <button type="button" ng-show="!isedit" class="btn btn-sm btn-success"  ng-click="edit(item)" style="margin: 5px">
                                                完成
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                <div class="panel-footer container-fluid">
                                    <div>总价：{{item.Weight | number:3}}*{{item.Price | number:2}}={{item.Weight*item.Price | number:2}}</div>
                                </div>
                            </div>
                            <div class="row text-left container-fluid">
                                <h4 style="display: inline-block">总价：{{orderdetaildata.Total | number:2}}</h4>
                            </div>
                            <div class="row text-right">
                                <button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">确认修改</button>
                                <button type="button" class="btn btn-default" ng-click="abolish()">取消修改</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
            //更改计划单商品控制器
            .controller("Purchasing_CommodityInfoEditC", function ($scope, $state, $stateParams, $http) {
                $scope.id = JSON.parse($stateParams.orderid);
                $scope.isedit = true;
                //获取计划单详细数据
                $http({
                    url: "../api/Purchase/" + $scope.id,
                    method: "GET"
                })
                        .then(function (res) {
                            $scope.orderdetaildata = res.data;
                            console.log($scope.orderdetaildata);
                        })
                        .then(function (error) {
                            if (error) {
                                console.log("error");
                            }
                        });
                $scope.edit = function (item) {
                    $scope.isedit = !$scope.isedit;
                    $scope.sum();
                };
                //计算总价方法
                $scope.sum = function () {
                    $scope.orderdetaildata.Total = 0;
                    for (var i = 0; i < $scope.orderdetaildata.Goodses.length; i++) {
                        $scope.orderdetaildata.Total += $scope.orderdetaildata.Goodses[i].Count * $scope.orderdetaildata.Goodses[i].Price * $scope.orderdetaildata.Goodses[i].Weight;
                    }
                };
                //保存更改
                $scope.ordersub = function () {
                    $http.post('../api/Purchase/SetPurchasingPrice', $scope.orderdetaildata)
                            .success(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    alert("保存成功");
                                    $state.go('app.Purchasing_Detail', {
                                        orderid: JSON.stringify($scope.id)
                                    });
                                }
                            });
                };
                //取消更改
                $scope.abolish = function () {
                    $state.go('app.Purchasing_Detail', {
                        orderid: JSON.stringify($scope.id)
                    });
                }

            })
</script>